package com.moore.Servlet;

import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;
import com.moore.utils.FileUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Random;

public class FileUploadServlet extends HttpServlet {

    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String base;//获取的数据str
        try {
            base = URLDecoder.decode(request.getParams().get("base64"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        // 获取上传文件的内容输入流
        String path = "html/images/upload/";
        Random random = new Random();
        int num = random.nextInt(100000000);
        String imageName = String.valueOf(num);//获取图片名
        String filePath = FileUtils.baseToImage(base, path, imageName);//获取路径
        assert filePath != null;
        return filePath.getBytes();
    }
}
